package cn.earas.ip;

import org.apache.http.client.config.*;
import org.apache.http.client.methods.*;
import org.apache.http.util.*;
import org.json.JSONObject;
import org.apache.http.client.*;

import java.io.*;

import org.apache.http.impl.client.*;
import org.apache.http.*;
import cn.earas.ip.frame.*;

public class NetCheck implements Runnable {
    final HttpClientBuilder build = HttpClients.custom();
    final CloseableHttpClient httpClient = build.build();
    int timeFlag;
    boolean more;
    int threads;

    long flag = 0;

    public NetCheck(final int timeFlag, boolean more, int threads) {
        this.timeFlag = timeFlag;
        this.more = more;
        this.threads = threads;
    }

    private Long doGet(final String ip, final int port) {
        Long result = null;
        final HttpHost proxy = new HttpHost(ip, port);
        final RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(timeFlag).setSocketTimeout(timeFlag).setConnectionRequestTimeout(timeFlag).build();
        final HttpGet get = new HttpGet("http://www.baidu.com");
        get.setConfig(requestConfig);
        try {
            final long start = System.currentTimeMillis();
            final HttpEntity entity = httpClient.execute(get).getEntity();
            final long end = System.currentTimeMillis();
            final String html = EntityUtils.toString(entity);
            if (html.contains("baidu")) {
                result = end - start;
            }
        } catch (ClientProtocolException ex) {
        } catch (IOException ex2) {
        }
        return result;
    }

    private DataModel doGet2(final String ip, final int port) {
        DataModel model = new DataModel();
        model.setIp(ip);
        model.setPort(port);
        Long result = null;
        final HttpHost proxy = new HttpHost(ip, port);
        final RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(timeFlag).setSocketTimeout(timeFlag).setConnectionRequestTimeout(timeFlag).build();
        final HttpGet get = new HttpGet("http://www.baidu.com");
        get.setConfig(requestConfig);
        try {
            final long start = System.currentTimeMillis();
            final HttpEntity entity = httpClient.execute(get).getEntity();
            final long end = System.currentTimeMillis();
            final String html = EntityUtils.toString(entity);
            if (html.contains("baidu")) {
                result = end - start;
            }
            model.setTime(result);
            try {
                Thread.sleep(100 * threads + flag);
            } catch (InterruptedException e) {
            }
            HttpGet get2 = new HttpGet("http://ip.aliyun.com/service/getIpInfo.php?ip=" + ip);
            final RequestConfig requestConfig2 = RequestConfig.custom().setConnectTimeout(timeFlag).setSocketTimeout(timeFlag).setConnectionRequestTimeout(timeFlag).build();
            get2.setConfig(requestConfig2);
            final HttpEntity entity2 = httpClient.execute(get2).getEntity();
            final String html2 = EntityUtils.toString(entity2);
            try {
                JSONObject json = new JSONObject(html2);
                JSONObject data = json.getJSONObject("data");
                String area = (data.getString("country").equals("XX") == true ? "" : data.getString("country")) + (data.getString("region").equals("XX") == true ? "" : data.getString("region")) + (data.getString("city").equals("XX") == true ? "" : data.getString("city"));
                String isp = data.getString("isp");
                model.setArea(area);
                model.setIsp(isp);
            } catch (Exception e) {
                flag = flag + 100;
            }
        } catch (ClientProtocolException ex) {
        } catch (IOException ex2) {
        }
        return model;
    }

    private Long doGet3(final String ip, final int port) {
        Long result = null;
        final HttpHost proxy = new HttpHost(ip, port);
        final RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(timeFlag).setSocketTimeout(timeFlag).setConnectionRequestTimeout(timeFlag).build();
        final HttpGet get = new HttpGet("https://www.baidu.com");
        get.setConfig(requestConfig);
        try {
            final long start = System.currentTimeMillis();
            final HttpEntity entity = httpClient.execute(get).getEntity();
            final long end = System.currentTimeMillis();
            final String html = EntityUtils.toString(entity);
            if (html.contains("baidu")) {
                result = end - start;
            }
        } catch (ClientProtocolException ex) {
        } catch (IOException ex2) {
        }
        return result;
    }

    @Override
    public void run() {
        while (!Data.ips.isEmpty() && Data.run) {
            final String data = Data.ips.remove(0);
            ++Data.checkedNum;

            boolean tempFlag = true;
            if (this.more) {
                DataModel model = null;
                switch (Data.proxyType){
                    case 0:{
                         model = this.doGet2(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        break;
                    }
                    case 1:{
                        model = this.doGet2(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        break;
                    }
                    case 2:{
                        model = this.doGet2(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        Long result2 = this.doGet3(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        if(null == result2 || result2 > this.timeFlag) tempFlag = false;
                        break;
                    }
                    default:break;
                }
                if (!Data.run) break;
                if (null != model.getTime() && model.getTime() <= this.timeFlag && tempFlag) {
                    Data.result2.add(model);
                    if (model.getIsp() != null)
                        MainFrame.jTextArea1.append(model.getIp() + ":" + model.getPort() + "，" + model.getTime() + "ms" + (model.getIsp().equals("XX") == true ? "" : ("，" + model.getIsp())) + (model.getArea().equals("") == true ? "" : ("，" + model.getArea())) + "。\n");
                    else
                        MainFrame.jTextArea1.append(model.getIp() + ":" + model.getPort() + "，" + model.getTime() + "ms。\n");
                }
            } else {
                Long result = null;
                switch (Data.proxyType){
                    case 0:{
                        result = this.doGet(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        break;
                    }
                    case 1:{
                        result = this.doGet3(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        break;
                    }
                    case 2:{
                        result = this.doGet(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        Long result2 = this.doGet3(data.substring(0, data.indexOf(":")), Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                        if(null == result2 || result2 > this.timeFlag) tempFlag = false;
                        break;
                    }
                    default:break;
                }

                if (!Data.run) break;
                if (null != result && result <= this.timeFlag) {
                    DataModel model = new DataModel();
                    model.setIp(data.substring(0, data.indexOf(":")));
                    model.setPort(Integer.valueOf(data.substring(data.indexOf(":") + 1)));
                    model.setTime(result);
                    Data.result2.add(model);
                    MainFrame.jTextArea1.append(data + "，响应时间：" + result + "毫秒。\n");
                }
            }
            if (Data.ips.isEmpty()) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
